﻿/*
 * SpotiFAG - Makes spotify ads less annoying
 * Copyright (C) 2010  ed <tripflag@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * (version 2) as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, refer to the following URL:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace spotifag {
    class Deprecated {

        Spotify app;
        bool ad = false;

        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        const byte VK_VOLUME_UP = 0xaf;
        const byte VK_VOLUME_DOWN = 0xae;
        // fuck that, spotify doesn't hook vk_volume_* as I thought.

        [DllImport("user32.dll")]
        static extern void SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_SYSKEYUP = 0x105;
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        // that didn't handle ctrl correctly, let's try something else

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
        const int VK_CONTROL = 17; //0xa2;
        const int VK_DOWN = 0x28;
        const int VK_UP = 0x26;
        // DAMNIT MICROSOFT GO FUCK A SHIT UP YOUR ASS

        private void muter(bool mute) {
            // Check whether we're actually changing mute state or not
            if (mute != ad) {
                ad = mute;
                IntPtr hwnd = app.hwnd();
                int coed = mute ? VK_DOWN : VK_UP;
                SendMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0);
                PostMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0);
                SendMessage(hwnd, WM_SYSKEYDOWN, VK_CONTROL, 0);
                PostMessage(hwnd, WM_SYSKEYDOWN, VK_CONTROL, 0);
                //Application.DoEvents();
                //System.Threading.Thread.Sleep(200);
                for (int a = 0; a < 5; a++) {
                    PostMessage(hwnd, WM_KEYDOWN, coed, 0);
                    PostMessage(hwnd, WM_KEYUP, coed, 0);
                    //SendMessage(hwnd, WM_KEYDOWN, coed, 0);
                    //SendMessage(hwnd, WM_KEYUP, coed, 0);
                }
                PostMessage(hwnd, WM_SYSKEYUP, VK_CONTROL, 0);
                SendMessage(hwnd, WM_SYSKEYUP, VK_CONTROL, 0);
                PostMessage(hwnd, WM_KEYUP, VK_CONTROL, 0);
                SendMessage(hwnd, WM_KEYUP, VK_CONTROL, 0);
            }
        }

        private void muter_system(bool mute) {
            // Check whether we're actually changing mute state or not
            if (mute != ad) {
                for (int a = 0; a < 5; a++) {
                    byte coed = mute ? VK_VOLUME_DOWN : VK_VOLUME_UP;
                    keybd_event(coed, 0, 0, 0);
                    keybd_event(coed, 0, 2, 0);
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
    }
}
